package com.fpx.metadata.job;

import com.fpx.metadata.bean.po.DbInfo;
import com.fpx.metadata.enmu.DBType;
import com.fpx.metadata.service.DbInfoService;
import com.fpx.metadata.service.handle.KettleMetaDataHandle;
import com.google.common.collect.Lists;
import lombok.extern.log4j.Log4j2;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Log4j2
@Service
public class KettleJob extends QuartzJobBean {

    @Autowired
    private DbInfoService dbInfoService;
    @Autowired
    private KettleMetaDataHandle kettleMetaDataHandle;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("kettle 任务开始");
        DbInfo infoWhere = new DbInfo();
        infoWhere.setType(DBType.KETTLE.code);
        List<DbInfo> dbInfos = dbInfoService.queryListByWhere(infoWhere);
        if (CollectionUtils.isEmpty(dbInfos)) {
            log.info("kettle 任务 没有需要爬取的目标");
            return ;
        }
        dbInfos.parallelStream().forEach(dbInfo -> {
            try {
                kettleMetaDataHandle.buildMetaDataInfo(dbInfo);
            } catch (Exception e) {
                log.error("kettle 任务失败", e);
            }
        });
    }
}
